package com.example.financecollegeorderdish.ui.dialog

import android.content.Context
import android.graphics.Color
import android.widget.TextView
import com.bumptech.glide.Glide
import com.drake.brv.utils.models
import com.drake.brv.utils.setup
import com.example.financecollegeorderdish.R
import com.example.financecollegeorderdish.databinding.DialogDetailBinding
import com.example.financecollegeorderdish.db.bean.DishBean
import com.google.android.flexbox.FlexboxLayoutManager
import com.google.gson.Gson
import com.lihang.ShadowLayout
import com.lxj.xpopup.core.CenterPopupView
import com.wuhao.lib_common.utils.LoggerHelper
import java.util.Random

/**
 * 旋转靶机
 */
class DetailDialog(context: Context, var itemBean: DishBean) : CenterPopupView(context) {
    val mBinding: DialogDetailBinding by lazy { DialogDetailBinding.bind(contentView) }

    /**
     * 具体实现的类的布局
     *
     * @return
     */
    override fun getImplLayoutId(): Int = R.layout.dialog_detail

    /**
     * do init.
     */
    override fun onCreate() {
        super.onCreate()
        mBinding.tvTitle.text = itemBean.name

        initRv()
        Glide.with(context)
            .load(itemBean.imagePath)
            .into(mBinding.dishImg)
        mBinding.name.text = itemBean.name
        mBinding.introduce.text = itemBean.introduce
        mBinding.cookMethod.text = itemBean.cookMethod
        mBinding.cookSkill.text = itemBean.cookSkill
        mBinding.rvRawMaterial.models =
            Gson().fromJson<Array<String>>(itemBean.rawMaterial, Array<String>::class.java).toList()

        mBinding.heat.text = "${itemBean.heat}千焦"
        mBinding.protein.text = "${itemBean.protein}克"
        mBinding.calorie.text = "${itemBean.calorie}千卡"
        mBinding.dietaryfiber.text = "${itemBean.dietaryFiber}克"

        mBinding.fat.text = "${itemBean.fat}克"
        mBinding.purine.text = "${itemBean.purine}毫克"
        mBinding.sugar.text = "${itemBean.sugar}克"

        mBinding.vitaminb1.text = "${itemBean.VitaminB1}毫克"
        mBinding.vitaminb2.text = "${itemBean.VitaminB2}毫克"
        mBinding.vitaminb3.text = "${itemBean.VitaminB3}毫克"
        mBinding.vitaminb5.text = "${itemBean.VitaminB5}毫克"
        mBinding.vitaminb6.text = "${itemBean.VitaminB6}毫克"
        mBinding.vitaminb7.text = "${itemBean.VitaminB7}毫克"
        mBinding.vitaminb9.text = "${itemBean.VitaminB9}毫克"
        mBinding.vitaminb12.text = "${itemBean.VitaminB12}毫克"
        mBinding.vitaminc.text = "${itemBean.VitaminC}毫克"
        mBinding.vitamina.text = "${itemBean.VitaminA}毫克"
        mBinding.vitamind.text = "${itemBean.VitaminD}毫克"
        mBinding.vitamine.text = "${itemBean.VitaminE}毫克"
        mBinding.vitamink.text = "${itemBean.VitaminK}毫克"

        mBinding.iron.text= "${itemBean.iron}毫克"
        mBinding.manganese.text= "${itemBean.manganese}毫克"
        mBinding.zinc.text= "${itemBean.zinc}毫克"
        mBinding.copper.text= "${itemBean.copper}毫克"
        mBinding.potassium.text= "${itemBean.potassium}毫克"
        mBinding.phosphorus.text= "${itemBean.phosphorus}毫克"
        mBinding.sodium.text= "${itemBean.sodium}毫克"
        mBinding.calcium.text= "${itemBean.calcium}毫克"
        mBinding.selenium.text= "${itemBean.selenium}毫克"
        mBinding.magnesium.text= "${itemBean.magnesium}毫克"

    }

    fun initRv() {
        mBinding.rvRawMaterial.apply {
            layoutManager = FlexboxLayoutManager(context)
            setup {
                addType<String>(R.layout.rv_item_tag_nodelete)
                onBind {
                    val tag = findView<TextView>(R.id.tag)
                    val sd = findView<ShadowLayout>(R.id.bg)
                    sd.setLayoutBackground(randomColor())
                    val model = getModel<String>()
                    tag.text = model
                }
            }.models = arrayListOf()
        }
    }


    fun randomColor(): Int {
        val random = Random()
        val red = random.nextInt(256)
        val green = random.nextInt(256)
        val blue = random.nextInt(256)
        LoggerHelper.e("red is:$red,green is:$green,blue is:$blue")
        return Color.rgb(red, green, blue)
    }
}